/****使用malloc()函数分配内存
void *malloc(size_t size); ****/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main(int argc,char *argv[])
{
	struct st
	{
		int n;
		struct st *next;
	}*q;

	int *p = NULL;
//	printf("p = %p\n",p);
	p = (int *)malloc(sizeof(int)*99);
	printf("p = %p\n",p);
	p = (int *)malloc(sizeof(int)*100);
	printf("p = %p\n",p);
	if(p == NULL)
	{
		printf("内存申请失败\n");
	}
	else
		printf("内存申请成功\n");
	q = (struct st*)malloc(sizeof(struct st));
	if(q == NULL)
	{
		printf("内存申请失败\n");
	}
	q->n = 5;
	q->next = NULL;
	printf("q->n=%d\tq->next=%x\n",q->n,q->next);
}

/****使用calloc()函数分配内存
void *calloc(size_t numElements,size_t sizeOfElement); ****/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main(int argc,char *argv[])
{
	int n,*p,*q;
	printf("输入数据的个数:\n");
	scanf("%d",&n);//4
	p = calloc(n,2);//p = 00920E28+4*2*4
	if(p == NULL)
	{
		printf("内存申请失败");
	}
	printf("p = %p\n",p);//p = 00920E28
	p = (int *)calloc(n,2);
	printf("p = %p\n",p);
	for(q = p;q < p+n;q++)
	{
		scanf("%d",q);
		printf("%4d",*q);
	}
	free(p);
//	printf("p = %p\n",p);会报错
	printf("\n");
}

/****为具有三个数组元素的数组分配内存****/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main(int argc,char *argv[])
{
	int *p;
	int i;
	p = (int *)malloc(sizeof(int[3]));
	if(p == NULL)
		printf("申请内存失败\n");
	for(i = 0;i < 3;i++)
	{
		*(p+i) = 10*(1+i);
		printf("%d\n",*(p+i));
	}
	return 0;
}


/****为二维数组动态分配内存****/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main(int argc,char *argv[])
{
	int **p;
	int index1,index2;
	p = (int **)malloc(sizeof(int*[3]));//先申请三个指针数组
//	if(p ==　NULL)为什么不能在这里验证
//		printf("申请动态内存失败\n");
	for(index1 = 0;index1 < 3;index1++)
	{
		*(p + index1) = (int *)malloc(sizeof(int[3]));//再申请三个数组元素
		for(index2 = 0;index2 < 3;index2++)
		{
			*(*(p+index1)+index2) = index1+index2;
		}
	}
	for(index1 = 0;index1 < 3;index1++)
	{
		for(index2 = 0;index2 < 3;index2++)
		{
			printf("(*(p+%d)+%d) = %d\t",index1,index2,*(*(p+index1)+index2));	
		}
		printf("\n");
	}
}

/****商品信息的动态存放****/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main(int argc,char *argv[])
{
	struct com{
		int num;
		char *name;
		int count;
		double price;
	}*information;
	information = (struct com *)malloc(sizeof(struct com));
	information->num = 1001;
	information->name = "apple";
	information->count = 100;
	information->price = 4.5;
	printf("information->num = %d\ninformation->name= %d\n
		information->count= %d\ninformation->price= %d\n",
		information->num,information->name,information->count,
		information->price);
}

/****使用二进制数特定位翻转****/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main(int argc,char *argv[])
{
	int result,a,b;
	b = 15;//0000 1111
	scanf("%d",&a);
	result = a^b;
	printf("result = %d\n",result);
}

/****左移两位输出****/

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int main(int argc,char *argv[])
{
	int result,a,b;
	b = 15;//0000 1111
	scanf("%d",&a);
	result = a<<2;
	printf("result = %d\n",result);
}

